﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SmokinAces.Object_Classes.Objects
{
    public class Flower:Game_Object, Draw_Object, Logic_Object
    {
        //------this is information for the editor...
        static Type[] constructor_parameters = {typeof(int),typeof(int), typeof(Color), typeof(int), typeof(Main_Engine)};
        static object[] default_const_values = {0,0,Color.BlueViolet,0,null};
        //------------------------------------
        Vector3 root = Vector3.Zero;
        Matrix rot = Matrix.Identity;
        VertexPositionColorTexture[] baseImage;
        VertexPositionColorTexture[] maskImage;
		int[] baseIndecies;
		int[] maskIndecies;
        int baseImageTexture;
        int maskImageTexture;
        Sort_Delegate m_Sort_DelegateBase;
        Sort_Delegate m_Sort_DelegateMask;
        #region Properties
        //Properties
        private int _RotationSpeed;
        public int RotationSpeed
        {
            get { return _RotationSpeed;}
            set 
            {
                rot = Matrix.CreateFromYawPitchRoll(0.0f, 0.0f, 0.01f + (value / 1000.0f));
                lastTime = 0;
                _RotationSpeed = value;
            }
        }

        private Color _Set_Color;
        public Color Set_Color
        {
            get
            {
                return _Set_Color;
            }
            set
            {
                maskImage[0].Color = value;
                maskImage[1].Color = value;
                maskImage[2].Color = value;
                maskImage[3].Color = value;
            }
        }
        #endregion
        public Flower(int left, int top, Main_Engine Parent)
        {
            Initialize(left, top, left + 16, top + 16, Color.BlueViolet, 0, Parent);
        }
        public Flower(int left, int top, Color color, int rotationSpeed, Main_Engine parent)
        {
            Initialize(left, top, left + 16, top + 16, color, rotationSpeed, parent);
        }
        public Flower(XmlReader xmlReader, Main_Engine Parent)
        {
            xmlReader.Read();
            string sLeft = xmlReader.ReadElementString("Left");
            string sTop = xmlReader.ReadElementString("Top");
            string sColor = xmlReader.ReadElementString("Color");
            string sRotation = xmlReader.ReadElementString("RotationSpeed");
            //----------------Extract the Colors
            string[] Splitter = {"{","}","R:","G:","B:","A:"," "};
            string[] ColorArray = sColor.Split(Splitter, StringSplitOptions.RemoveEmptyEntries);
            //------------------------------------
            int left = Convert.ToInt32(sLeft);
            int top = Convert.ToInt32(sTop);
            int RotationS = Convert.ToInt32(sRotation);
            Color tColor = new Color(Convert.ToByte(ColorArray[0]), Convert.ToByte(ColorArray[1]), Convert.ToByte(ColorArray[2]), Convert.ToByte(ColorArray[3]));
            Initialize(left, top, left + 16, top + 16, tColor,RotationS, Parent);
        }
        public override bool Save_XML(XmlTextWriter textWriter)
        {
            try
            {
                textWriter.WriteStartElement("Game_Object");
                textWriter.WriteAttributeString("Type", this.GetType().AssemblyQualifiedName);
                textWriter.WriteElementString("Left", baseImage[0].Position.X.ToString());
                textWriter.WriteElementString("Top", baseImage[0].Position.Y.ToString());
                textWriter.WriteElementString("Color", _Set_Color.ToString());
                textWriter.WriteElementString("RotationSpeed", _RotationSpeed.ToString());
                textWriter.WriteEndElement();
                textWriter.Flush();
            }
            catch
            {
                return false;
            }
            return true;
        }
        private void Initialize(int left, int top, int right, int bottom, Color color, int rotationSpeed, Main_Engine parent)
        {
            _Set_Color = color;
            const float zorder = 1.0f;
            this.parent = parent;
            GraphicBB = new BoundingBox(new Vector3(left, top, Util.MinZ), new Vector3(right, bottom, Util.MaxZ));
            float width = right - left;
            float height = bottom - top;

            Color baseColor = new Color(255,255,255,255);
            //color = baseColor;
            baseImage = new VertexPositionColorTexture[4];
            baseImage[0].Position = new Vector3(left, top, zorder);
            baseImage[0].Color = baseColor;
            baseImage[0].TextureCoordinate = new Vector2(1.0f, 1.0f);
            baseImage[1].Position = new Vector3(left, bottom, zorder);
            baseImage[1].Color = baseColor;
            baseImage[1].TextureCoordinate = new Vector2(1.0f, 0.0f);
            baseImage[2].Position = new Vector3(right, top, zorder);
            baseImage[2].Color = baseColor;
            baseImage[2].TextureCoordinate = new Vector2(0.0f, 1.0f);
            baseImage[3].Position = new Vector3(right, bottom, zorder);
            baseImage[3].Color = baseColor;
            baseImage[3].TextureCoordinate = new Vector2(0.0f, 0.0f);

            maskImage = new VertexPositionColorTexture[4];

            maskImage[0].Position = new Vector3(left, top, zorder);
            maskImage[0].Color = color;
            maskImage[0].TextureCoordinate = new Vector2(1.0f, 1.0f);

            maskImage[1].Position = new Vector3(left, bottom, zorder);
            maskImage[1].Color = color;
            maskImage[1].TextureCoordinate = new Vector2(1.0f, 0.0f);

            maskImage[2].Position = new Vector3(right, top, zorder);
            maskImage[2].Color = color;
            maskImage[2].TextureCoordinate = new Vector2(0.0f, 1.0f);

            maskImage[3].Position = new Vector3(right, bottom, zorder);
            maskImage[3].Color = color;
            maskImage[3].TextureCoordinate = new Vector2(0.0f, 0.0f);

            root = new Vector3(left + width / 2.0f, top + height / 2.0f, 1.0f);
            RotationSpeed = rotationSpeed;

			baseImage = Util.ComplexMesh(baseImage, out baseIndecies, 2, 2);
			maskImage = Util.ComplexMesh(maskImage, out maskIndecies, 2, 2);

            //---------------This adds the draw function to the appropriate layer
            baseImageTexture = parent.register_texture("Grass\\Stem.png");
            maskImageTexture = parent.register_texture("Grass\\Peddles.png");
            m_Sort_DelegateBase = new Sort_Delegate(((1024 - bottom) * 1024) + left, Draw);
            //m_Sort_DelegateMask = new Sort_Delegate(((1024 - bottom) * 1024) + left, Draw_Peddle);
        }

        #region Draw_Object Members

        public void Draw(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.GraphicsDevice device)
        {
            //---Set the Texture for this layer
            parent.effect.BaseTexture = parent.Texture_List[baseImageTexture];
            //---------Begin the drawing passes
            parent.effect.Begin();
            foreach (EffectPass pass in parent.effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                //device.DrawUserPrimitives(PrimitiveType.TriangleStrip, baseImage, 0, 2);
				device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, baseImage, 0, baseImage.Length, baseIndecies, 0, baseIndecies.Length/3);
                pass.End();
            }
            parent.effect.End();
            Draw_Peddle(gameTime, device);
        }
        public void Draw_Peddle(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Graphics.GraphicsDevice device)
        {
            //---Set the Texture for this layer
            parent.effect.BaseTexture = parent.Texture_List[maskImageTexture];
            //---------Begin the drawing passes
            parent.effect.Begin();
            foreach (EffectPass pass in parent.effect.CurrentTechnique.Passes)
            {
                pass.Begin();
                    device.DrawUserPrimitives(PrimitiveType.TriangleStrip, maskImage, 0, 2);
                pass.End();
            }
            parent.effect.End();
        }
        public bool startDraw()
        {
            parent.Draw_Functions[2].Add(m_Sort_DelegateBase);
            //parent.Draw_Functions[2].Add(m_Sort_DelegateMask);
            return true;
        }
        public bool stopDraw()
        {
            parent.Draw_Functions[2].Remove(m_Sort_DelegateBase);
            //parent.Draw_Functions[2].Remove(m_Sort_DelegateMask);
            return true;
        }
        #endregion

        #region Game_Object Members
        public override bool dispose()
        {
            return stopDraw() && stopUpdate();
        }

        #endregion

        #region Logic_Object Members
        int lastTime;
        public void Update(GameTime gameTime)
        {
            if (gameTime.TotalGameTime.Ticks - lastTime > 1)
            {
                maskImage[0].Position = Vector3.Add(Vector3.Transform(Vector3.Subtract(maskImage[0].Position, root), rot), root);
                maskImage[1].Position = Vector3.Add(Vector3.Transform(Vector3.Subtract(maskImage[1].Position, root), rot), root);
                maskImage[2].Position = Vector3.Add(Vector3.Transform(Vector3.Subtract(maskImage[2].Position, root), rot), root);
                maskImage[3].Position = Vector3.Add(Vector3.Transform(Vector3.Subtract(maskImage[3].Position, root), rot), root);
                lastTime = gameTime.TotalGameTime.Milliseconds;
            }
        }
        public bool startUpdate()
        {
            parent.add_Update_Object(Update);
            return true;
        }
        public bool stopUpdate()
        {
            parent.remove_Update_Object(Update);
            return true;
        }
        #endregion
    }
}
